package com.hengxinguotong.zhihuichengjian.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.AreaListRes;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;
import com.hengxinguotong.zhihuichengjian.widget.pullrefresh.PullToRefreshListView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 区域列表
 * Created by Administrator on 2017/7/20.
 */

public class AreaListActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    HXTextView ivBack;
    @BindView(R.id.area_lv)
    PullToRefreshListView areaLv;

    private List<AreaListRes.AreaBean> areaList;
    private AreaListActivity mActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_area_list);
        ButterKnife.bind(this);
        mActivity = this;
        getAreaList("加载中…");
        setListener();
    }

    private void getAreaList(String alert){

        Utils.requestData(alert, mActivity, "/construction/queryAuthAreaList/" + SPUtil.getString(mActivity, "uuid"), null, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                if(!Utils.isEmpty(json)){

                    Gson gson = new Gson();
                    AreaListRes areaListRes = gson.fromJson(json, AreaListRes.class);
                    if(areaListRes.getValue() != null && areaListRes.getValue().size()>0){
                        areaList = areaListRes.getValue();
                        areaLv.setAdapter(new MyAdapter());
                    }else{
                        showEmptyView(areaLv);
                    }
                }
            }

            @Override
            public void onFailure(String json) {
                    showEmptyView(areaLv);
            }
        });
    }

    private void setListener(){
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        areaLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mActivity, ConstructionListActivity.class);
                intent.putExtra("area", areaList.get(position - 1));
                startActivity(intent);
            }
        });
    }

    private class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return areaList.size();
        }

        @Override
        public Object getItem(int position) {
            return areaList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null || convertView.getTag() == null) {
                convertView = LayoutInflater.from(mActivity)
                        .inflate(R.layout.item_area, parent, false);
                holder = new ViewHolder();
                holder.areaName = (HXTextView)convertView.findViewById(R.id.area_name);
                holder.container = (RelativeLayout) convertView.findViewById(R.id.container);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.areaName.setText(areaList.get(position).getName());
            if(position%2 == 0){
                holder.container.setBackgroundResource(R.color.color_item_bg);
            }else{
                holder.container.setBackgroundResource(R.color.white_ffffff);
            }
            return convertView;
        }

        private class ViewHolder{
            RelativeLayout container;
            HXTextView areaName;
        }
    }
}
